<template>
  <div>


    <div class="pd-10">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>

      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div class="mg-10">
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline ml-5"></i></el-button>
      <el-popconfirm
          confirm-button-text='是的我确定'
          cancel-button-text='算了在想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定删除吗？"
          @confirm="delBatch"
          class="ml-5"
      >
        <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline ml-5"></i></el-button>
      </el-popconfirm>
    </div>

    <el-table
        :data="tableData" border stripe
        :header-cell-style="{ background:'#eee',border: '1px solid #ccc' ,color: '#000'}"
        @selection-change="handleSelectionChange"
        row-key="id"
        default-expand-all>
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="id" width="100" >
      </el-table-column>
      <el-table-column prop="name" label="名称" >
      </el-table-column>
      <el-table-column prop="path" label="路径" >
      </el-table-column>
      <el-table-column prop="pagePath" label="页面路径" >
      </el-table-column>
      <el-table-column label="图标" align="center" >
        <template slot-scope="scope">
            <i style="font-size: 30px" :class="scope.row.icon"/>
        </template>
      </el-table-column>
        <el-table-column prop="weight" label="权重">
      </el-table-column>
      <el-table-column prop="description" label="描述">
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope" >
          <el-button type="info" @click="handleAdd(scope.row.id)"  v-if="!scope.row.pid && !scope.row.path">新增子菜单<i class="el-icon-plus ml-5"></i></el-button>
          <el-button type="success" @click="handleEdit(scope.row)" >编辑<i class="el-icon-edit ml-5"></i></el-button>
          <el-popconfirm
              confirm-button-text='是的我确定'
              cancel-button-text='算了在想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="handledelete(scope.row.id)"
              class="ml-5"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-delete ml-5"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增用户" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form"  label-width="80px" size="small">
        <el-form-item label="名称" >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权重" >
          <el-input v-model="form.weight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径" >
          <el-input v-model="form.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="页面路径" >
          <el-input v-model="form.pagePath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" >
          <el-select v-model="form.icon"  clearable placeholder="请选择" style="width: 100%">
            <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
              <i :class="item.value"/> {{item.name}}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" >
          <el-input v-model="form.description"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "User",
  data(){
    return{
      tableData: [], //分页数据
      total: 0, //总条数
      name: "", //搜索框用户名
      form: {}, //表格数据
      multipleSelection: [], //多选删除
      dialogFormVisible: false, //新增按钮
      options: [],  //图标
      num: 1
    }
  },
  created() {
    this.load()
  },
  methods:{
    //分页请求
    load(){
      this.request.get("/menu",{
        params:{
          name: this.name,
        }
      }).then(res =>{
        if(res.code === "200"){
          this.tableData = res.data
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //分页方法
    handleSizeChange(pageSize) {
      this.pageSize=pageSize;
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load()
    },
    //重置搜索框
    reset(){
      this.name = "";
      this.load()
    },
    //新增按钮
    handleAdd(id){
      this.dialogFormVisible = true;
      this.form = {}
      this.request.get("/dict/icons").then(res =>{
        this.options = res.data
      })
      if(id!==null){  //当id存在时视为创建子菜单
        this.form.pid = id;
      }
    },
    //确定新增
    save(){
      let newVar = typeof this.form.pid;
      if(newVar !== "number"){
        this.form.pid = null;
      }

      this.request.post("/menu", this.form).then(res=>{
        if(res.code ==="200"){
          this.$message.success(res.data)
          this.form ={}
          this.dialogFormVisible = false;
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //编辑按钮
    handleEdit(row){
      this.form = row
      this.dialogFormVisible = true
      this.request.get("/dict/icons").then(res =>{
        this.options = res.data
      })
    },
    //取消按钮
    cancel() {
      this.dialogFormVisible=false
      this.load()
    },
    //删除按钮
    handledelete(id){
      this.request.delete("/menu/"+id).then(res=>{
        if(res.code === "200"){
          this.$message.success(res.data)
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    //批量删除多选
    handleSelectionChange(val){
      this.multipleSelection  = val
    },
    //点击批量删除
    delBatch(){
      let ids= this.multipleSelection.map(v => v.id)  //将数组内的id提取出来
      this.request.delete("/menu/del/batch",{
        data: ids
      }).then(res=>{
        if(res.code === "200"){
          this.$message.success(res.data)
          this.load()
        }else {
          this.$message.error(res.msg)
        }
      })
    },
  }
}
</script>

<style>
.fontSize18{
  font-size: 18px;
}
.fontSize12{
  font-size: 15px;
}
</style>